package vu.alfonsodiecko.stundenplanfhjena.gui;

import java.util.Calendar;

import org.holoeverywhere.widget.ProgressBar;

import vu.alfonsodiecko.stundenplanfhjena.OnlineHelper;
import vu.alfonsodiecko.stundenplanfhjena.Project;
import vu.alfonsodiecko.stundenplanfhjena.R;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;

public class Wetter extends ANotifyingScreenLoading {

	private ProgressBar progress;
	private TextView text;
	private int state = 1;
	private WebView webview;
	private String data = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (data == null) {
			setContentView(R.layout.loader1);
			progress = (ProgressBar) findViewById(R.id.progressBarLoad);
			text = (TextView) findViewById(R.id.textViewLoader);
			ActionBar navigation_bar = getSupportActionBar();
			// Setting standart navigation bar view
			navigation_bar.setTitle("Wetter");
			navigation_bar.setSubtitle("Wetterstadion EAH Jena");
			startProgress();
		} else {
			createMainWindow();
		}

	}

	private void createMainWindow() {
		setContentView(R.layout.fh_wetter);
		webview = (WebView) findViewById(R.id.webViewWetter);
		webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");
	}

	@Override
	boolean doInbackground() {
		data = OnlineHelper.getWetter(Calendar.getInstance(), Project.Websites.fhwetter);
		return true;
	}

	@Override
	boolean reloadGUI() {
		runOnUiThread(new Runnable() {
			public void run() {
				createMainWindow();
			}
		});
		return true;
	}

	@Override
	boolean setProgress() {
		return progress.post(new Runnable() {
			@Override
			public void run() {
				text.setText("Loading " + String.valueOf(state) + "s");
				progress.setProgress(state);
				state++;
			}
		});

	}

}
